package com.leoren.run.utils.mapApi;

import android.widget.EditText;

import com.amap.api.maps.model.LatLng;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.DriveStep;
import com.amap.api.services.route.RouteSearch;
import com.leoren.run.vo.RoutePath;
import com.leoren.run.vo.RunMapRouteVo;
import com.leoren.run.vo.RunRouteVo;
import com.leoren.run.vo.WayPoint;
import com.leoren.run.vo.apiVo.DriveRoute;
import com.leoren.run.vo.pojo.RunMap;
import com.leoren.run.vo.pojo.RunRoute;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import retrofit2.http.PATCH;

/**
 * 地图工具类
 */
public class AMapUtil {

    /**
     * 判断edittext是否null
     */
    public static String checkEditText(EditText editText) {
        if (editText != null && editText.getText() != null
                && !(editText.getText().toString().trim().equals(""))) {
            return editText.getText().toString().trim();
        } else {
            return "";
        }
    }

    public static boolean IsEmptyOrNullString(String s) {
        return (s == null) || (s.trim().length() == 0);
    }

    /**
     * 把LatLonPoint对象转化为LatLon对象
     */
    public static LatLng convertToLatLng(LatLonPoint latLonPoint) {
        return new LatLng(latLonPoint.getLatitude(), latLonPoint.getLongitude());
    }

    public static String getFriendlyTime(int second) {
        if (second > 3600) {
            int hour = second / 3600;
            int miniate = (second % 3600) / 60;
            return hour + "小时" + miniate + "分钟";
        }
        if (second >= 60) {
            int miniate = second / 60;
            return miniate + "分钟";
        }
        return second + "秒";
    }

    public static String getFriendlyLength(int lenMeter) {
        if (lenMeter > 10000) // 10 km
        {
            int dis = lenMeter / 1000;
            return dis + ChString.Kilometer;
        }

        if (lenMeter > 1000) {
            float dis = (float) lenMeter / 1000;
            DecimalFormat fnum = new DecimalFormat("##0.0");
            String dstr = fnum.format(dis);
            return dstr + ChString.Kilometer;
        }

        if (lenMeter > 100) {
            int dis = lenMeter / 50 * 50;
            return dis + ChString.Meter;
        }

        int dis = lenMeter / 10 * 10;
        if (dis == 0) {
            dis = 10;
        }

        return dis + ChString.Meter;
    }

    /**
     * 生成runMap对象
     * @param result
     * @param dis
     * @param timeInt
     */
    public static RunMapRouteVo generateRunMap(DriveRouteResult result, float dis, int timeInt, String province, String city, String district) {
        RunMapRouteVo runMap;
        runMap = new RunMapRouteVo();
        runMap.setProvince(province);
        runMap.setCity(city);
        runMap.setDistrict(district);
        LatLonPoint startPoint = result.getStartPos();
        LatLonPoint endPoint = result.getTargetPos();
        runMap.setOriginLat((float)startPoint.getLatitude());
        runMap.setOriginLng((float) startPoint.getLongitude());
        runMap.setDestinationLat((float) endPoint.getLatitude());
        runMap.setDestinationLng((float) endPoint.getLongitude());
        runMap.setWayPoints(result.getDriveQuery().getPassedByPoints().size());
        runMap.setDistince((int) dis);
        runMap.setTime(timeInt);
        runMap.setRunRouteVos(getRunRouteVoList(result.getPaths()));
        runMap.setWayPointList(getWayPoints(result));
        return runMap;
    }

    private static List<RunRouteVo> getRunRouteVoList(List<DrivePath> paths){
        DrivePath route = paths.get(0);
        List<DriveStep> steps = route.getSteps();
        List<RunRouteVo> runRouteVos = new ArrayList<>();
        for(int i = 0; i < steps.size(); i++){
            DriveStep step = steps.get(i);
            RunRouteVo runRoute = new RunRouteVo();
            runRoute.setStepCount(i);
            List<LatLonPoint> pathPoint = step.getPolyline();
            LatLonPoint startPoint = pathPoint.get(0);
            LatLonPoint endPoint = step.getPolyline().get(pathPoint.size() - 1);
            runRoute.setStartLat((float) startPoint.getLatitude());
            runRoute.setStartLng((float) startPoint.getLongitude());
            runRoute.setEndLat((float) endPoint.getLatitude());
            runRoute.setEndLng((float) endPoint.getLongitude());
            runRoute.setRouteDestince((int) step.getDistance());
            runRoute.setInstruction(step.getInstruction());
            runRoute.setStreet(step.getRoad());
            runRoute.setOrientation(step.getOrientation());
            runRoute.setRoutePaths(addRoutePath(step));
            runRouteVos.add(runRoute);
        }
        return runRouteVos;
    }

    private static List<RoutePath> addRoutePath(DriveStep step){
        List<RoutePath> routePaths = new ArrayList<>();
        List<LatLonPoint> points = step.getPolyline();
        for(int i = 0; i <  points.size(); i++){
            LatLonPoint point = points.get(i);
            RoutePath path = new RoutePath();
            path.setCount(i);
            path.setLat((float) point.getLatitude());
            path.setLng((float) point.getLongitude());
            path.setP((float) point.getLatitude());
            path.setQ((float) point.getLongitude());
            routePaths.add(path);
        }
        return routePaths;
    }

    private static List<WayPoint> getWayPoints(DriveRouteResult result){
        List<WayPoint> pointList = new ArrayList<>();
        RouteSearch.DriveRouteQuery query = result.getDriveQuery();
        List<LatLonPoint> passPoint = query.getPassedByPoints();
        for(int i = 0; i < pointList.size(); i++){
            LatLonPoint point = passPoint.get(i);
            pointList.add(new WayPoint(point.getLongitude(), point.getLatitude()));
        }
        return pointList;
    }
}
